package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PenaltyStrategyBean;
import com.koron.css2.baseConfig.mapper.PenaltyStrategyMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.types.ObjectId;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 违约金策略-添加
 * @author 李攀
 *
 */
public class PenaltyStrategyAdd implements ServerInterface{

	private static Logger logger = LogManager.getLogger(PenaltyStrategyAdd.class);

	@Override
    @ValidationKey(clazz = PenaltyStrategyBean.class,method = "insert")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
	MessageBean<PenaltyStrategyBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PenaltyStrategyBean.class);
	try {
	PenaltyStrategyBean bean = JsonUtils.objectToPojo(req.getData(), PenaltyStrategyBean.class);
	PenaltyStrategyMapper mapper = factory.getMapper(PenaltyStrategyMapper.class);
	// 校验字段重复
	if (mapper.check("strategy_name", bean.getStrategyName()) > 0) {
		info.setCode(Constant.MESSAGE_INT_FAIL);
		info.setDescription("违约金名称：" + bean.getStrategyName() + "的信息已存在。");
		return info;
	}
	bean.setPenaltyId(new ObjectId().toHexString());
	bean.setCreateName(userInfo.getUserInfo().getName());
	bean.setCreateTime(CommonUtils.getCurrentTime());

	mapper.insertPenaltyStrategy(bean);
	} catch (Exception e) {
		info.setCode(Constant.MESSAGE_INT_FAIL);
		info.setDescription(e.getMessage());
		logger.error(e.getMessage(), e);
	}
	return info;
}
}